

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

/**
 * Servlet implementation class for Servlet: GetMap
 *
 */
 public class GetMap extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public GetMap() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		
		long starttime = new Date().getTime();
		//System.out.println("Start: " + starttime);
		
		
		// Uebergabeparameter abholen
		String mode = request.getParameter("mode");
		String size = request.getParameter("size");
		String scale = request.getParameter("scale");
		String orientation = request.getParameter("orientation");
		String title = request.getParameter("title");
		String timestamp = request.getParameter("timestamp");
		String datalayers = request.getParameter("datalayers");
		String bbox = request.getParameter("BBOX");
		
		//System.out.println(bbox);
		
		String layout = "1";
		
		mapObj myMap = new mapObj(layout, mode, size, scale, orientation, title, timestamp, datalayers, bbox);
		
		//myMap.setMarginTitle(200);
		
		
		try {
		
			myMap.createPDFDocument();
		
			myMap.createUrls();
			myMap.addImages();
			myMap.addFrame();
			myMap.addArrow();
			myMap.addTitle();
			
			// maschenweite wahrscheinlich etwas wie scale/10 oder so...
			//myMap.printCoords(200);
			
			myMap.closePDFDocument();
			
			System.out.println("*** finito ***");
		
			  // PDF abspeichern und via redirect in den Browser holen
	    	response.setContentType("text/html");
	    	PrintWriter out = response.getWriter();	    	
	    	out.println("<html>");
	    	out.println("<head>");
	    	//out.println("<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=http://www.sogis1.so.ch/sogis/gifs/myMap"+timestamp+".pdf\">");
	    	//out.println("<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=http://srsofaioi12288/sogis/gifs/myMap"+timestamp+".pdf\">");
	    	out.println("<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=http://localhost/workspace/tmp/myMap"+timestamp+".pdf\">");
	    	out.println("<title>Karte drucken</title>");
	        out.println("</head>");
	        out.println("<body>");
	        out.println("&nbsp;");
	        out.println("</body>");
	        out.println("</html>");		
	        
	        //System.out.println("Ende: " + (new Date().getTime() - starttime) );
	        //System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
	        //System.out.println("");
	        
			
		
		} catch(DocumentException de) {
	    	de.printStackTrace();
	    	System.err.println("document: " + de.getMessage());
		}
		
	}  	  	  	    
}